package web.reclamos.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import web.reclamos.beans.Coorporacion;
import web.reclamos.beans.Detalle;
import web.reclamos.beans.Empresa;
import web.reclamos.beans.Factura;
import web.reclamos.beans.Reclamo;
import web.reclamos.beans.Tipo;
import web.reclamos.service.EmpresaService;
import web.reclamos.service.ReclamoService;

public class ReclamosServlet extends HttpServlet {


    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        if(request.getServletPath().equals("/reclamo_nuevo.reclamo")) {
            nuevoReclamo(request, response);
        }else if(request.getServletPath().equals("/reclamo_modificar.reclamo")) {
            modificarReclamo(request, response);
        }else if(request.getServletPath().equals("/reclamo_consultar.reclamo")) {
            consultarReclamo(request, response);
        }
    }

    private void nuevoReclamo(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
          ReclamoService recService = new ReclamoService();
          Reclamo reclamo = new Reclamo();
            System.out.println("+++++++++++++++++++++++++++");            
            System.out.println("cliente: "+request.getParameter("txtCliente"));
            System.out.println("cod_recla: "+request.getParameter("txtCodigoReclamo"));
//            System.out.println("tipo recla: "+request.getParameter("txtTipoReclamo"));
            System.out.println("factura : "+request.getParameter("txtFactura"));
            System.out.println("detalle: "+request.getParameter("txtDetalle"));
            System.out.println("select escogido: "+request.getParameter("txtTipoSelect"));
            System.out.println("+++++++++++++++++++++++++++");          
          reclamo.setCodigo_reclamo(request.getParameter("txtCodigoReclamo"));
          reclamo.setTipo(new Tipo(Integer.parseInt(request.getParameter("txtTipoSelect")), null));
          reclamo.setFactura(new Factura(0, request.getParameter("txtFactura"), null, null));
          reclamo.setDetalle(request.getParameter("txtDetalle"));

           try {
                recService.registrarReclamo(reclamo);
            }catch(Exception ex) {
                //Ir pagina de error
            }
           //Regresar pagina producto_nuevo
          response.sendRedirect("tipo_listar.reclamo");
    }
    private void modificarReclamo(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        /*ProductoService proService = new ProductoService();
        String filtro = request.getParameter("txtFiltro");

        Producto producto = new Producto();
        producto.setId(Integer.parseInt(request.getParameter("txtId")));
        producto.setNombre(request.getParameter("txtNombre"));
        producto.setPrecio(Float.parseFloat(request.getParameter("txtPrecio")));
        producto.setStock(Integer.parseInt(request.getParameter("txtStock")));

        proService.modificarProducto(producto);

        String url = "/producto_consultar.isil?txtNombre=" + filtro ;
        getServletContext().getRequestDispatcher(url).forward(request, response);*/
    }
  
    private void consultarReclamo(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        ReclamoService recService = new ReclamoService();
        String nombre = request.getParameter("txtNombre");

        List<Reclamo> reclamos = recService.consultarPorNombre(nombre);

        request.setAttribute("nombre", nombre);
        request.setAttribute("reclamos", reclamos);

        String url = "/ReclamoTodos.jsp";
        getServletContext().getRequestDispatcher(url).forward(request, response);

    }
}
